/*
** 2840.c
** 
** Made by (frank)
** Login   <frank@FrankChen>
** 
** Started on  Thu Oct  2 10:39:10 2008 frank
** Last update Sun May 12 01:17:25 2002 Speed Blue
*/

/* #include "2840.h" */
#include <stdio.h>

int main()
{
     int instance;
     char clock_time[6];
     char len, strikes;			/* save space */

     scanf("%d", &instance);
     while(instance){
	  scanf("%s", clock_time);
	  for(len = 0; clock_time[len] != '\0'; len++)
	       clock_time[len] -= '0';
	  if (len == 4)
	       if (clock_time[2] + clock_time[3]) /* not o'clock */
		    strikes = 0;
	       else strikes = clock_time[0] + 12; /* '0' == 48 */
	  else if (len == 5)
	       if (clock_time[3] + clock_time[4]) /* not o'clock */
		    strikes = 0;
	       else {
		    strikes = clock_time[0]*10 + clock_time[1] + 12;
		    if (strikes > 24)
			 strikes -= 24;
	       }
	  printf("%d\n", strikes);
	  instance--;
     }
     return 0;
}

	  
		    
	       
